package com.jdjc.auth.domin.service.impl;


import com.jdjc.auth.common.enums.AuthPermissionStatusEnum;
import com.jdjc.auth.common.enums.IsDeletedFlagEnum;
import com.jdjc.auth.domin.convert.AuthPermissionDominConverter;
import com.jdjc.auth.domin.entity.AuthRolePermissionBO;
import com.jdjc.auth.domin.service.AuthRolePermissionDominService;
import com.jdjc.auth.infra.basic.entity.AuthPermission;
import com.jdjc.auth.infra.basic.entity.AuthRolePermission;
import com.jdjc.auth.infra.basic.service.AuthRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


@Service("AuthRolePermissionDominService")
@Slf4j
public class AuthRolePermissionDominServiceImpl implements AuthRolePermissionDominService {
    @Resource
    private AuthRolePermissionService authRolePermissionService;

    /**
     * 新增角色权限关联关系
     * @param authRolePermissionBO
     * @return
     */
    @Override
    public Boolean add(AuthRolePermissionBO authRolePermissionBO) {
        if (log.isInfoEnabled()) {
            log.info(" AuthRolePermissionDominServiceImpl：add:BO:{}", authRolePermissionBO);
        }
        //批量插入
        List<AuthRolePermission> authRolePermissionList = new LinkedList<>();
        Long roleId = authRolePermissionBO.getRoleId();
        //遍历将permissionId分别放入不同的对象然后一次性插入
        authRolePermissionBO.getPermissionIdList().forEach(permissionId -> {
            AuthRolePermission authRolePermission=new AuthRolePermission();
            authRolePermission.setRoleId(roleId);
            authRolePermission.setPermissionId(permissionId);
            authRolePermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            authRolePermissionList.add(authRolePermission);
        });
        //批量插入
        int count = authRolePermissionService.insertBatch(authRolePermissionList);
        return count>0;
    }
}
